package com.leon.androidcoursedesign.ui.dialogs;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.leon.androidcoursedesign.R;

public class NoteDialog extends AlertDialog implements View.OnClickListener {
    private EditText note_editText_note_name;

    private final String dialogTitle;
    private final boolean isEdit;

    public NoteDialog(@NonNull Context context, String dialogTitle, boolean isEdit) {
        super(context);
        this.dialogTitle = dialogTitle;
        this.isEdit = isEdit;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_dialog);

        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        TextView course_textView_title = findViewById(R.id.note_textView_title);
        course_textView_title.setText(dialogTitle);

        note_editText_note_name = findViewById(R.id.note_editText_note_name);

        // 如果该dialog为修改课程使用的话该值不能为null

        Button note_button_cancel = findViewById(R.id.note_button_cancel);
        note_button_cancel.setOnClickListener(this);

        Button note_button_submit = findViewById(R.id.note_button_submit);
        note_button_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_button_cancel:
                dismiss();
                break;
            case R.id.note_button_submit:
                String noteTitle = note_editText_note_name.getText().toString();
                if (noteTitle.equals("")) {
                    Toast.makeText(getContext(), "请输入笔记标题", Toast.LENGTH_SHORT).show();
                    break;
                }

                // 判断是否为修改笔记标题
                if (isEdit) {
                    if (onCallback != null) {
                        onCallback.callBackTitle(noteTitle);
                    }
                } else {
                    if (onCallback != null) {
                        onCallback.callBackTitle(noteTitle);
                        onCallback.saveNote();
                    }
                }

                dismiss();
                break;
            default:
                break;
        }
    }

    private OnCallback onCallback;

    public interface OnCallback {
        /**
         * 传递修改|保存的笔记标题
         *
         * @param noteTitle 笔记标题
         */
        void callBackTitle(String noteTitle);

        /**
         * 保存笔记内容到本地
         */
        void saveNote();
    }

    public void setOnCallback(OnCallback onCallback) {
        this.onCallback = onCallback;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
